package xgocache

import (
	"context"
	"gitee.com/go-mid/infra/xcache/cacheconf"
	"github.com/patrickmn/go-cache"
	"time"
)

//基于内存的本地缓存，
type GoCache struct {
	*GoCacheConfig
	client *cache.Cache
}
type GoCacheConfig struct {
	cacheconf.CacheConfig
	DefaultExpireMs int64 `json:"default_expire_ms"`
}

func NewGoCache(ctx context.Context, conf *GoCacheConfig) *GoCache {

	client := cache.New(time.Duration(conf.DefaultExpireMs)*time.Millisecond, time.Duration(conf.DefaultExpireMs)*time.Millisecond)
	return &GoCache{
		GoCacheConfig: conf,
		client:        client,
	}
}
func (m *GoCache) Set(ctx context.Context, key string, val interface{}, exp time.Duration) (s string, err error) {
	m.client.Set(key, val, exp)
	return "", nil
}

//内存中的对象，没有序列化，因此返回的类型，就是存入的数据类型。
func (m *GoCache) Get(ctx context.Context, key string) (s interface{}, err error) {
	v, _ := m.client.Get(key)
	return v, nil
}

func (m *GoCache) Del(ctx context.Context, key string) (n int64, err error) {
	return 0, nil
}

func (m *GoCache) Close(ctx context.Context) (err error) {
	return nil
}
